package xyz.magicjson.core.example;

import xyz.magicjson.core.annotation.ESBEndpoint;
import xyz.magicjson.core.exception.ESBException;
import xyz.magicjson.core.model.Message;
import xyz.magicjson.core.model.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 示例控制器，演示ESBEndpoint和Result的使用
 */
@RestController
@RequestMapping("/api")
public class ExampleController {

    /**
     * 成功请求的示例
     */
    @ESBEndpoint(name = "getUser")
    @GetMapping("/users/{id}")
    public Result<User> getUser(@PathVariable("id") Long id) {
        if (id <= 0) {
            throw new ESBException(400, "Invalid user ID");
        }
        
        User user = new User();
        user.setId(id);
        user.setName("User " + id);
        user.setAge(20 + id.intValue());
        
        return Result.success(user);
    }
    
    /**
     * 返回消息对象的示例
     */
    @ESBEndpoint(name = "getMessage")
    @GetMapping("/messages/{id}")
    public Result<Message> getMessage(@PathVariable("id") String id) {
        Message message = new Message();
        message.setId(id);
        message.setPayload("Hello from ESB!");
        message.addHeader("Content-Type", "text/plain");
        message.addMetadata("source", "test");
        
        return Result.success(message);
    }
    
    /**
     * 抛出异常的示例
     */
    @ESBEndpoint(name = "errorExample")
    @GetMapping("/error")
    public Result<Void> errorExample() {
        throw new ESBException(500, "Internal server error example");
    }
    
    /**
     * 直接返回非Result对象的示例
     */
    @ESBEndpoint(name = "directReturn")
    @GetMapping("/direct")
    public Object directReturn() {
        return "This will be wrapped in a Result object by the aspect";
    }
} 